package xframe.util.tld;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.TagSupport;
import xframe.util.*;
import xframe.util.db.DBConnector;

/**
 * Created by IntelliJ IDEA.
 * User: kbs
 * Date: 2005. 11. 15
 * Time: ���� 3:54:07
 * To change this template use File | Settings | File Templates.
 */
public class SelectBox extends TagSupport {

    private String xmlName ="";
    private String selectValue="";
    private String selectText="";
    private String htmlName="";
    private String onChange="";
    private String id="";
    private String className="";
    private String size="";
    private String defaultValue=null;
    private String defaultText=null;
    private String type="";


	public int doStartTag()		throws JspException
    {
        try {
            HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
            HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();

            JspWriter out = pageContext.getOut();

            try {
                out.print(getSelectString(request));
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (XMLQueryException e) {
                e.printStackTrace();
            }
        } catch(IOException ioe) {
            ioe.printStackTrace();

        }
        return SKIP_BODY;
    }

    private  String getSelectString( HttpServletRequest request) throws SQLException, XMLQueryException {
        setXmlName(xmlName);
        setSelectValue(selectValue);
        setSelectText(selectText);
        setHtmlName(htmlName);
        setOnChange(onChange);
        setId(id);
        setClassName(className);
        setSize(size);
        setDefaultValue(defaultValue);
        setDefaultText(defaultText);
        setType(type);

        String return_value="<SELECT name='"+htmlName+"' size ='"+size+"' class='"+className+"' id ='"+id+"' onChange='"+onChange+"'>\n";
        if(defaultValue != null && defaultText != null){
               return_value +="<option value='"+defaultValue+"'>"+defaultText+"</option>\n";
        }

        String query =  XMLInMemory.get(xmlName);
        if(type.equalsIgnoreCase("hash")){
              query = VTLTranslator.getDecode(query, (HashMap)request.getAttribute("selectbox"));
        }else if(type.equalsIgnoreCase("request")){
              query = VTLTranslator.getDecode(query, request);
        }else if(type.equalsIgnoreCase("string")){
             query  = (String)request.getAttribute("selectbox");
        }

        Connection con  =null;
        Statement stmt  = null;
        ResultSet rs = null;
        ResultSetMetaData rsmt=null;
        try {
            con = DBConnector.getConnection();
            stmt = con.createStatement();
            rs = stmt.executeQuery(query);
            rsmt =  rs.getMetaData();
            ArrayList LIST  = new ArrayList();

            while(rs.next()){
                 HashMap hm =new HashMap();
                for(int i= 1; i < rsmt.getColumnCount()+1; i++){
                    hm.put(rsmt.getColumnName(i), rs.getString(i));
                }
                LIST.add(hm);
            }

            HashMap hm =new HashMap();
            for(int i =0 ; i < LIST.size() ; i++ ){
                hm = (HashMap)LIST.get(i);
                return_value += "<option value='"+hm.get(selectValue)+"'>"+hm.get(selectText)+"</option>\n";
            }
            return_value +="</select>" ;
            
           

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();  
        } finally {
            if(con != null) con.close();
        }

        return return_value;
    }

    public void setXmlName(String xmlName) {
        this.xmlName = xmlName;
    }

    public void setSelectValue(String selectValue) {
        this.selectValue = selectValue;
    }

    public void setSelectText(String selectText) {
        this.selectText = selectText;
    }

    public void setHtmlName(String htmlName) {
        this.htmlName = htmlName;
    }

    public void setOnChange(String onChange) {
        this.onChange = onChange;
    }

	public void setId(String id) {
        this.id = id;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public String getDefaultText() {
        return defaultText;
    }

    public void setDefaultText(String defaultText) {
        this.defaultText = defaultText;
    }

    public void setType(String type) {
        this.type = type;
    }
}


